package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.content.common.utils.UAASecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService courseBaseService;


    /**
     * 分页查询课程基本信息
     *
     * @param params 分页查询参数
     * @param model  查询条件
     * @return 分页查询对象
     */
    @PostMapping("course/list")
    public PageVO queryCourseBaseList(PageRequestParams params, @RequestBody QueryCourseModel model) {
        // 1.获得请求头中的令牌并解析拿到companyId值
        Long companyId = UAASecurityUtil.getCompanyId();

        // 2.调用service层获得结果数据
        PageVO pageVO = courseBaseService.queryCourseBaseList(params, model, companyId);
        return pageVO;
    }

    /**
     * 添加课程基本信息
     *
     * @param courseBaseVO 前端传递的数据对象
     * @return 创建的课程基本信息
     */
    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //获取教育机构id
        Long companyId = UAASecurityUtil.getCompanyId();
        //将vo转换为dto对象
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        courseBaseDTO.setCompanyId(companyId);
        //获取结果对象并返回
        CourseBaseDTO courseBase = this.courseBaseService.createCourseBase(courseBaseDTO);
        return courseBase;
    }

    /**
     * 更新录播课程
     *
     * @param courseBaseVO 前端传递的对象
     * @return 更新的课程基本信息
     */
    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //获取公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        //vo转换dto
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        courseBaseDTO.setCompanyId(companyId);
        //获取结果并返回
        CourseBaseDTO baseDTO = this.courseBaseService.modifyCourseBase(courseBaseDTO);
        return baseDTO;
    }

    /**
     * 根据课程id查询课程基本信息
     *
     * @param courseBaseId 课程id
     * @return 课程基本信息
     */
    @GetMapping(value = "course/{courseBaseId}")
    public CourseBaseDTO getCourseBaseById(@PathVariable Long courseBaseId) {
        //获取公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        //获取结果并返回
        CourseBaseDTO baseDTO = this.courseBaseService.getById(courseBaseId, companyId);
        return baseDTO;
    }

    /**
     * 根据课程id修改课程基本信息的删除状态
     *
     * @param courseBaseId 课程id
     */
    @DeleteMapping("course/{courseBaseId}")
    public void removeCourseBaseById(@PathVariable Long courseBaseId) {
        //获取机构id
        Long companyId = UAASecurityUtil.getCompanyId();
        //修改课程的删除信息
        this.courseBaseService.removeCourseBaseById(courseBaseId, companyId);
    }

    /**
     * 教育机构提交课程基本信息
     *
     * @param courseBaseId 课程id
     */
    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        //获取机构id
        Long companyId = UAASecurityUtil.getCompanyId();
        //提交课程基本信息,修改审核状态
        this.courseBaseService.commitCourseBase(courseBaseId, companyId);
    }

    /**
     * 课程预览
     * @param courseBaseId 课程id
     * @param companyId 机构id
     * @return 数据模型
     */
    @Override
    @GetMapping("course/preview/{courseBaseId}/{companyId}")
    public Object preview(@PathVariable Long courseBaseId, @PathVariable Long companyId) {
        Map<String, Object> dataMap = this.courseBaseService.preview(courseBaseId, companyId);
        ModelAndView modelAndView = new ModelAndView("learing_article", dataMap);
        return modelAndView;
    }

    /**
     * 课程发布
     * @param courseBaseId 课程id
     */
    @Override
    @PostMapping("/course_pub/publish/{courseBaseId}")
    public void publish(@PathVariable Long courseBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        this.courseBaseService.publish(courseBaseId, companyId);
    }
}
